/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import org.ipti.br.tag.util.EncryptionUtil;

/**
 *
 * @author israel
 */
@Entity
public class Address extends AbstractEntity{
    private static final long serialVersionUID = 1L;

    private String number;
    
    @Column(length = 60) 
    private String complement;
    
    @ManyToOne
    @JoinColumn(name="neighborhood_id",nullable=false)
    private Neighborhood neighborhood;
    
    @ManyToOne
    @JoinColumn(name="street_id",nullable=false)
    private Street street;
    
    @ManyToOne
    @JoinColumn(name="zipcode_id",nullable=false)
    private Zipcode zipcode;
    
    public Address() {
    }

    @Override
    public void generateHash(){
        if(getId() == null){
            String addressHashString = ""
                    + ((this.getZipcode() != null) ? this.getZipcode().getId() : null)
                    + ((this.getNeighborhood() != null) ? this.getNeighborhood().getId() : null)
                    + ((this.getStreet() != null) ? this.getStreet().getId() : null)
                    + this.getNumber()
                    + this.getComplement();
            this.setId(Long.valueOf(EncryptionUtil.encrypt(addressHashString).hashCode()));
        }
    }

    public Address(Neighborhood neighborhood, Street street, Zipcode zipcode) {
        this.neighborhood = neighborhood;
        this.street = street;
        this.zipcode = zipcode;
    }

    public Address(String number, String complement, Neighborhood neighborhood, Street street, Zipcode zipcode) {
        this.number = number;
        this.complement = complement;
        this.neighborhood = neighborhood;
        this.street = street;
        this.zipcode = zipcode;
    }

    public String getComplement() {
        return complement;
    }

    public void setComplement(String complement) {
        this.complement = complement;
    }

    public Neighborhood getNeighborhood() {
        return neighborhood;
    }

    public void setNeighborhood(Neighborhood neighborhood) {
        this.neighborhood = neighborhood;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public Street getStreet() {
        return street;
    }

    public void setStreet(Street street) {
        this.street = street;
    }

    public Zipcode getZipcode() {
        return zipcode;
    }

    public void setZipcode(Zipcode zipcode) {
        this.zipcode = zipcode;
    }
}
